package test.blued.com.casecontroller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import test.blued.com.casecontroller.commenthod.CommandMethod;
import test.blued.com.casecontroller.myInterface.HttpJson;

/**
 * Created by BlueCity on 2017/8/22.
 */

public class Auto extends Activity{

    private CommandMethod method = new CommandMethod();
    private GridView gridview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto);
        Intent intent = getIntent();
        String software = intent.getStringExtra("software");
        casesCMDStr = getSoftware(software);
        findView();

        adapterGridView();

    }

    public void adapterGridView(){
        //生成动态数组，并且转入数据
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < images.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", images[i]);//添加图像资源的ID
            map.put("ItemText", names[i]);//按序号做ItemText
            lstImageItem.add(map);
        }
        //生成适配器的ImageItem 与动态数组的元素相对应
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                lstImageItem,    //数据来源
                R.layout.item_grid,     //item的XML实现
                new String[]{"ItemImage", "ItemText"},    //动态数组与ImageItem对应的子项
                new int[]{R.id.img_item, R.id.text_item});   //ImageItem的XML文件里面的一个ImageView,两个TextView ID
        //添加并且显示
        gridview.setAdapter(saImageItems);
        //添加消息处理
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                method.theCMD(casesCMDStr[position]);
            }
        });
    }

    public void findView(){
        gridview = (GridView)findViewById(R.id.auto_grid);
    }

    public String[] getSoftware(String str){
        if(str.equals("home")){
            String[] casesCMDStr = {
                    "am instrument --user 0 -w -r -e package test.cases.wang -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r -e package test.cases.message -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r -e package test.cases.discovery -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r -e package test.cases.live -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r -e package test.cases.my -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.cases -e debug false " +
                            "com.soft.blued.test/test.communal.InstrumentationTestRunner"
            };
            return casesCMDStr;
        }else if(str.equals("international")){
            String[] casesCMDStr = {
                    "am instrument --user 0 -w -r  -e package test.testCases.nearyCases -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.testCases.meng_case -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.testCases.discoverCases -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.testCases.liveCases -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.testCases.LiuCases -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner",
                    "am instrument --user 0 -w -r  -e package test.testCases -e debug false " +
                            "com.blued.international.test/test.communal.InstrumentationTestRunner"
            };
            return casesCMDStr;
        }else {
            Toast.makeText(Auto.this, "can\'t find the software :" + str, Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    private int[] images = {
            R.drawable.tab_find_btn,
            R.drawable.tab_msg_btn,
            R.drawable.tab_feed_btn,
            R.drawable.tab_live_btn,
            R.drawable.tab_personal_btn,
            R.mipmap.ic_launcher};
    private String[] names = {
            "neary",
            "message",
            "discover",
            "live",
            "personal",
            "all"};
    private String[] casesCMDStr = new String[images.length];
}
